﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Web.DynamicData;
using System.IO;

namespace badminton.DynamicData.FieldTemplates
{
    public partial class Image_EditField : System.Web.DynamicData.FieldTemplateUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected string extractImageName()
        {
            if (FileUpload1.HasFile && FileUpload1.FileName != "" && getSavedFilePath(FileUpload1.FileName)!="")
            {
                return GetFileName(getSavedFilePath(FileUpload1.ToolTip));
            }
            return HyperLink1.Text;
        }
        protected override void ExtractValues(IOrderedDictionary dictionary)
        {
            dictionary[Column.Name] = ConvertEditedValue(extractImageName());
        }

        public override Control DataControl
        {
            get
            {
                return HyperLink1;
            }
        }

        public string GetFileName(string str)
        {
            str = str.Trim();
            if (str.Length == 0)
            {
                return str;
            }
            int idx = str.LastIndexOf('\\');
            if (idx == -1) idx = str.LastIndexOf('/');

            if (idx == -1) return str;

            return str.Substring(idx + 1);

        }

        public string GetLink(string str)
        {
            string fn = GetFileName(str);
            if (fn.Length == 0) return "";

            return @"~\DynamicData\images\" + fn;
        }

        protected string getSavedFilePath(string filePath)
        {
            if (filePath.Trim() == "") return "";

            string fileName = filePath.Substring(filePath.LastIndexOf("\\ ") + 1);
            string type = fileName.Substring(fileName.LastIndexOf(".")).ToLower();
            if (type != ".jpg" && type != ".bmp" && type != ".gif")
            {
                return "";
            }

            string dir = Server.MapPath("~\\DynamicData\\images\\") ;
            string fnWithouttype = fileName.Substring(0, fileName.LastIndexOf('.'));

            string postfix = "";
            int num = 0;
            while (File.Exists(dir + fnWithouttype + postfix + type))
            {
                num = num + 1;
                postfix = num.ToString();
            }

            return dir + fnWithouttype + postfix + type;
        }

        protected void OnLoad(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {

                string fullFileName = this.FileUpload1.PostedFile.FileName;
                string savePath = getSavedFilePath(fullFileName);

                if (savePath != "")
                {
                    this.FileUpload1.SaveAs(savePath);
                    this.FileUpload1.ToolTip = savePath;
                }
                else
                {
                    Response.Write("<script language= 'javascript '> alert('您上传的图片格式不对');  </script> ");
                }
            }

            return;
        }
    }
}
